package com.atguigu.tingshu.common.delay;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class DelayMssage {
    @Autowired
    private RedissonClient redissonClient;
    public void sendDelayMsg(String queueName, Object msg, Long delayTime) {
                try {
                    RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(queueName);
                    RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
                    delayedQueue.offer(msg, delayTime, TimeUnit.SECONDS);
                } catch (Exception e) {
                    log.error("发送延迟消息失败,msg:" + msg.toString());
                    throw new RuntimeException(e);
                }


    }
}
